package com.qy.sss.service;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SanshishuService{
	
	@Autowired
	public JdbcTemplate jdbcTemplate;
	
	/**
	 * 前世福禄
	 * @param nian
	 * @param yue
	 * @return
	 * @throws SQLException 
	 */
	public String qianshi_fulu(String nian,String yue){
		int year = (Integer.parseInt(nian) - 1900) % 12 + 1;
		String para = String.valueOf(year*12 + Integer.parseInt(yue));
		String sql = "select pre,this from qianshi_fulu,para where para.para= ? and para.id=qianshi_fulu.id"; 
		
		Map ret = jdbcTemplate.queryForMap(sql, new Object[]{para});		
		
		String ret1 = "<font color=red><strong>前世诗曰:</strong></font><br>"+ret.get("pre");
		String ret2 = "<font color=red><strong>现世诗曰:</strong></font><br>"+ret.get("this");
		return ret1+"<br/>"+ret2;
	}
	
	/**
	 * 前世寿命
	 * @param nian
	 * @param yue
	 * @return
	 */
	public String qianshi_shouming(String nian,String yue){
		int year = (Integer.parseInt(nian) - 1900) % 12 + 1;
		String para = String.valueOf(year*12 + Integer.parseInt(yue));
		String sql = "select pre,this from qianshi_shouming,para where para.para= ? and para.id=qianshi_shouming.id"; 
		
		Map ret = jdbcTemplate.queryForMap(sql, new Object[]{para});		
		
		String ret1 = "<font color=red><strong>前世诗曰:</strong></font><br>"+ret.get("pre");
		String ret2 = "<font color=red><strong>现世诗曰:</strong></font><br>"+ret.get("this");
		return ret1+"<br/>"+ret2;
	}
	
	/**
	 * 前世手艺
	 * @param nian
	 * @param yue
	 * @return
	 */
	public String qianshi_shouyi(String nian,String yue){
		int year = (Integer.parseInt(nian) - 1900) % 12 + 1;
		String para = String.valueOf(year*12 + Integer.parseInt(yue));
		String sql = "select pre,this from qianshi_shouyi,para where para.para= ? and para.id=qianshi_shouyi.id"; 
		
		Map ret = jdbcTemplate.queryForMap(sql, new Object[]{para});		
		
		String ret1 = "<font color=red><strong>前世诗曰:</strong></font><br>"+ret.get("pre");
		String ret2 = "<font color=red><strong>现世诗曰:</strong></font><br>"+ret.get("this");
		return ret1+"<br/>"+ret2;
	}
	/**
	 * 前世
	 * @param nian
	 * @param yue
	 * @return
	 */
	public String qianshi_tuohua(String nian,String yue){
		int year = (Integer.parseInt(nian) - 1900) % 12 + 1;
		String sql = "select pre from qianshi_tuohua where id = ?"; 
		Map ret = jdbcTemplate.queryForMap(sql, new Object[]{year});		
		
		String ret1 = "<font color=red><strong>前世诗曰:</strong></font><br>"+ret.get("pre");
		String str = "&nbsp;&nbsp;&nbsp;&nbsp;万物在五行的相生相克中轮回繁衍生息。人类是阴界和阳界的中介，" +
				"是阴气和阳气相碰撞的产物，所以人死了就会一部分留在阴间而另一部分留在阳间，" +
				"这就是一部一个“你”或“他”，而这个过程就叫做托化轮回。托化轮回是自然选择的结果，" +
				"是必然之中的偶然。<br/>";
		return str+ret1;
	}
	/**
	 * 现世
	 * @param nian
	 * @param yue
	 * @return
	 */
	public String xianshi_caiyun_jiankang(String nian,String yue){
		int year = (Integer.parseInt(nian) - 1900) % 12 + 1;
		String para = String.valueOf(year*12 + Integer.parseInt(yue));
		String sql = "select caiyun,jiankang from xianshi_caiyun_jiankang,para where para.para = ? and para.id=xianshi_caiyun_jiankang.id"; 
		
		Map ret = jdbcTemplate.queryForMap(sql, new Object[]{para});		
		
		String ret1 = "<font color=red>财运部分。</font><br/>"+ret.get("caiyun");
		String ret2 = "<font color=red>健康部分。</font><br>"+ret.get("jiankang");
		return ret1+"<br/>"+ret2;
	}
	/**
	 * 现世
	 * @param nian
	 * @param yue
	 * @return
	 */
	public String xianshi_guansha(String nian,String yue){
		String ret = "";
		Calendar cal=Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int id = (year - (Integer.parseInt(nian) - 1)) % 12+1;
		if(id <= 0){
			return "你是未来人啊？";
		}else{
			switch(id){
				case 1:
					ret = "<font color=red>太岁:</font>太岁当头坐 无喜祸必来 凡是应谨慎 可免血之灾  ";
					break;
				case 2:
					ret = "<font color=red>太阳:</font>太阳吉星照 福害也变财 家家坐百福 户户纳千祥   ";
					break;
				case 3:
					ret = "<font color=red>丧门:</font>丧门入命来 孝服又归来 若然不小心 破财即见灾  ";
					break;
				case 4:
					ret = "<font color=red>太阴:</font>太阴入命来 女性最为宜 添喜又添财 贵人接引来  ";
					break;
				case 5:
					ret = "<font color=red>官符:</font>官符入命来 口舌是非多 做个失败奴 只好番慎做  ";
					break;
				case 6:
					ret = "<font color=red>死符:</font>死符入命来 孝服重重逢 父母无刑克 作福免灾殃";
					break;
				case 7:
					ret = "<font color=red>岁破:</font>岁破入命来 破财总难免 慎防小人害 诸事多不宜";
					break;
				case 8:
					ret = "<font color=red>月德:</font>日德入命来 四季进横财 在家相安好 出门无兄灾";
					break;
				case 9:
					ret = "<font color=red>白虎:</font>白虎入命来 破财挡大灾 更主有官非 疾病飞来害 ";
					break;
				case 10:
					ret = "<font color=red>福德:</font>福德入命来 吉星照床前 添户进人口 财喜两全周 ";
					break;
				case 11:
					ret = "<font color=red>吊客:</font>吊客入命来 恐有灾害至 凡事要慎重 也可免大灾";
					break;
				case 12:
					ret = "<font color=red>病符:</font>病符入命来 疾病灾害来 官非乍来犯 作福免灾殃 ";
					break;
			}
		}
		return ret;
	}
	/**
	 * 现世 家庭
	 * @param nian
	 * @param yue
	 * @return
	 */
	public String xianshi_jiating(String nian,String yue){
		String ret = "";
		int year = (Integer.parseInt(nian) - 1864) % 60 + 1;
		int month = Integer.parseInt(yue);
		
		String strsql="select nayin from xianshi_shengchen_nian where id=?";
		Map map = jdbcTemplate.queryForMap(strsql, new Object[]{year});	
		
		int nayin = (Integer)map.get("nayin");
		String para = String.valueOf((nayin-1)*12 + month);
		
		strsql="select fuqi,hunyin,xiongdi,liuqin from para_jiating,xianshi_jiating where " +
				"para_jiating.para_jiating = ? and para_jiating.id=xianshi_jiating.id";
		map = jdbcTemplate.queryForMap(strsql, new Object[]{para});
		ret += "<font color=red>夫妻:</font>"+map.get("fuqi")+"<br>";
		ret += "<font color=red>婚姻:</font>"+map.get("hunyin")+"<br>";
		ret += "<font color=red>兄弟:</font>"+map.get("xiongdi")+"<br>";
		return ret+map.get("liuqin");
	}
	
	/**
	 * 现世命宫
	 * @param nian
	 * @param yue
	 * @return
	 */
	public String xianshi_minggong(String nian,String yue){
		int year = (Integer.parseInt(nian) - 1900) % 12 + 1;
		String para = String.valueOf(year*12 + Integer.parseInt(yue));
		String sql = "select this_0,this_1,this_2,this_3 from xianshi_minggong,para_minggong where para_minggong.para_minggong = ? and para_minggong.id=xianshi_minggong.id";
		
		Map ret = jdbcTemplate.queryForMap(sql, new Object[]{para});		
		
		String ret1 = "<font color=red><strong>命宫诗曰:</strong></font><br>"+ret.get("this_3");
		String ret2 = "<font color=red><strong>星曜诗曰:</strong></font><br>"+ret.get("this_1");
		String ret3 = "<font color=red><strong>说明:</strong></font><br>"+ret.get("this_2");
		return ret.get("this_0")+ret1+"<br/>"+ret2;
	}
	
	/**
	 * 现世
	 * @param nian
	 * @param yue
	 * @return
	 */
	public String xianshi_shengchen(String nian,String yue,String ri,String shi){
		String ret = "";
		int year = (Integer.parseInt(nian) - 1864) % 60 + 1;
		int month = Integer.parseInt(yue);
		int day = Integer.parseInt(ri);
		int time = Integer.parseInt(shi);
		
		String strsql="select taisui_0,this,taisui,mingyun from xianshi_shengchen_nian where id = ?";
		Map map = jdbcTemplate.queryForMap(strsql, new Object[]{year});	
		ret += map.get("this") + "<br/><font color=red>"+map.get("taisui_0")
		+":</font>"+map.get("taisui")+"<br>"+"<font color=red>命运：</font>"+map.get("mingyun")+"<br>";
		
		strsql="select this from xianshi_shengchen_yue where id = ?";
		map = jdbcTemplate.queryForMap(strsql, new Object[]{month});	
		ret += map.get("this")+"<br/>";
		
		strsql="select this from xianshi_shengchen_ri where id = ?";
		map = jdbcTemplate.queryForMap(strsql, new Object[]{ri});	
		ret += map.get("this")+"<br/>";
		
		strsql="select this,fore,mid,after from xianshi_shengchen_shi where id = ?";
		map = jdbcTemplate.queryForMap(strsql, new Object[]{shi});	
		//内容太多 去掉子时3个文字片段
		//ret += map.get("this")+"<br/>"+map.get("fore")+"<br/>"+map.get("mid")+"<br/>"+map.get("after")+"<br/>";
		ret += map.get("this")+"<br/>";
		
		return "<div style='font-size:14px;'>"+ret+"</div>";
	}
	
	/**
	 * 现世
	 * @param nian
	 * @param yue
	 * @return
	 */
	public String xianshi_shouming(String nian,String yue){
		int year = (Integer.parseInt(nian) - 1864) % 60 + 1;
		String sql = "select nayin from xianshi_shengchen_nian where id = ?";
		
		Map ret = jdbcTemplate.queryForMap(sql, new Object[]{year});		
		String nayin = String.valueOf(ret.get("nayin"));
		int ny = Integer.parseInt(nayin) - 1;
		String param = "" + (ny*12 + Integer.parseInt(yue));
		sql = "select short,lg from xianshi_shouming,para_jiating where para_jiating.para_jiating = ? and para_jiating.id=xianshi_shouming.id";
		ret = jdbcTemplate.queryForMap(sql, new Object[]{param});	
		return "您最短寿命为<font color=red><strong>"+ret.get("short")+"</strong></font>岁<br>您最长寿命为<font color=red><strong>" + ret.get("lg")+"</strong></font>岁";
	}
	/**
	 * 现世
	 * @param nian
	 * @param yue
	 * @return
	 */
	public String xianshi_simasha(String nian,String yue){
		int year = (Integer.parseInt(nian) - 1900) % 12 + 1;
		String para = String.valueOf(year*12 + Integer.parseInt(yue));
		String sql = "select this from xianshi_simasha,para where para.para = ? and para.id=xianshi_simasha.id";
		
		Map ret = jdbcTemplate.queryForMap(sql, new Object[]{para});		
		return (String)ret.get("this");
	}

	public String laishi_difang(String nian, String yue) {
		int year = (Integer.parseInt(nian) - 1900) % 12 + 1;
		String para = String.valueOf(year*12 + Integer.parseInt(yue));
		String sql = "select after from laishi_difang,para where para.para = ? and para.id=laishi_difang.id";
		
		Map ret = jdbcTemplate.queryForMap(sql, new Object[]{para});		
		return "诗曰:"+(String)ret.get("after"); 
	}

	public String laishi_mingyun(String nian, String yue) {
		int year = (Integer.parseInt(nian) - 1900) % 12+ 1;
		String para = String.valueOf(year*12 + Integer.parseInt(yue));
		String sql = "select after from laishi_mingyun,para where para.para = ? and para.id=laishi_mingyun.id";
		
		Map ret = jdbcTemplate.queryForMap(sql, new Object[]{para});		
		return "诗曰:"+(String)ret.get("after"); 
	}

	public String laishi_shengxiao(String nian, String yue) {
		int year = (Integer.parseInt(nian) - 1900) % 12+ 1;
		String sql = "select after from qianshi_tuohua where id = ?";
		
		Map ret = jdbcTemplate.queryForMap(sql, new Object[]{year+1});		
		return "万物在五行的相生相克中轮回繁衍生息。人类是阴界和阳界的中介，是阴气和阳气相碰撞的产物，所以人死了就会一部分留在阴间而另一部分留在阳间，这就是一部一个“你”或“他”，而这个过程就叫做托化轮回。托化轮回是自然选择的结果，是必然之中的偶然。"+
		"<br/>诗曰:"+(String)ret.get("after"); 
	}
	
	public List<Map<String,Object>> selectTable(String table){
		return jdbcTemplate.queryForList("select * from " + table);
	}
	
	public int update(String tab,String id,String[]params){
		String del = "delete from "+ tab +" where id = '"+id+"'";
		jdbcTemplate.update(del);
		String sql = "insert into "+tab+" values('"+StringUtils.join(params, "','")+"')";
		return jdbcTemplate.update(sql);
	}
}
